﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace BO.Global
{
    class sealize
    {
        #region ISerializationFormatter Members

        /// <summary>
        /// Converts a serialization stream into an
        /// object graph.
        /// </summary>
        /// <param name="serializationStream">
        /// Byte stream containing the serialized data.</param>
        /// <returns>A deserialized object graph.</returns>
        //public object Deserialize(System.IO.Stream serializationStream)
        //{
        //    return _formatter.Deserialize(serializationStream);
        //}
        public object Deserialize(System.IO.Stream serializationStream)
        {
            //return _formatter.Deserialize(serializationStream);

            //MemoryStream msNew = new MemoryStream(bytes);
            //serializationStream.Position = 0;
            using (GZipStream gzipStream = new GZipStream(serializationStream, CompressionMode.Decompress))
            {
                byte[] buffer = new byte[4096];//定义数据缓冲  
                int offset = 0;//定义读取位置  
                object obj;
                using (MemoryStream ms = new MemoryStream())  //定义内存流  
                {
                    while ((offset = gzipStream.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        ms.Write(buffer, 0, offset);//解压后的数据写入内存流  
                    }
                    //BinaryFormatter sfFormatter = new BinaryFormatter();//定义BinaryFormatter以反序列化object对象  
                    ms.Position = 0;//设置内存流的位置  



                    obj = (object)_formatter.Deserialize(ms);//反序列化  

                    ms.Close();//关闭内存流  
                }

                gzipStream.Close();//关闭解压缩流  
                //gzipStream.Dispose();//释放资源  
                //msNew.Close();
                //msNew.Dispose();
                return obj;
            }
        }

        /// <summary>
        /// Converts an object graph into a byte stream.
        /// </summary>
        /// <param name="serializationStream">
        /// Stream that will contain the the serialized data.</param>
        /// <param name="graph">Object graph to be serialized.</param>
        //public void Serialize(System.IO.Stream serializationStream, object graph)
        //{
        //    _formatter.Serialize(serializationStream, graph);
        //}
        private IFormatter _formatter =
       new BinaryFormatter();
        public void Serialize(System.IO.Stream serializationStream, object graph)   //tianyun
        {
            byte[] buffer = null;
            using (MemoryStream ms = new MemoryStream()) //;//创建内存流对象  
            {
                _formatter.Serialize(ms, graph);//把object对象序列化到内存流  
                buffer = ms.ToArray();//把内存流对象写入字节数组  
                ms.Close();//关闭内存流对象  
            }

            using (GZipStream gzipStream = new GZipStream(serializationStream, CompressionMode.Compress, true)) //;//创建压缩对象  
            {
                gzipStream.Write(buffer, 0, buffer.Length);//把压缩后的数据写入文件  
                gzipStream.Close();//关闭压缩流,这里要注意：一定要关闭，要不然解压缩的时候会出现小于4K的文件读取不到数据，大于4K的文件读取不完整              
            }

        }

        #endregion
    }
}
